/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.partyassessment.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.modules.assessment.entity.AssessmentTime;
import com.fosung.frame.modules.assessment.service.AssessmentTimeService;
import com.fosung.frame.modules.partyassessment.entity.PartyAssessment;
import com.fosung.frame.modules.partyassessment.service.PartyAssessmentService;
import com.fosung.frame.modules.partyassessmentinfo.entity.PartyAssessmentInfo;
import com.fosung.frame.modules.partyassessmentinfo.service.PartyAssessmentInfoService;
import com.fosung.frame.modules.partytype.entity.PartyType;
import com.fosung.frame.modules.partytype.service.PartyTypeService;
import com.fosung.frame.modules.personassessmen.entity.PersonAssessment;

/**
 * 党组织考核Controller
 * @author wu
 * @version 2017-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/partyassessment/partyAssessment")
public class PartyAssessmentController extends BaseController {

	@Autowired
	private PartyAssessmentService partyAssessmentService;
	@Autowired
	private AssessmentTimeService assessmentTimeService;
	@Autowired
	private PartyTypeService partyTypeService;
	@Autowired
	private PartyAssessmentInfoService partyAssessmentInfoService;
	
	@ModelAttribute
	public PartyAssessment get(@RequestParam(required=false) String id) {
		PartyAssessment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = partyAssessmentService.get(id);
		}
		if (entity == null){
			entity = new PartyAssessment();
		}
		return entity;
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:view")
	@RequestMapping(value = {"list", ""})
	public String list(PartyAssessment partyAssessment, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PartyAssessment> page = partyAssessmentService.findPage(new Page<PartyAssessment>(request, response), partyAssessment); 
		model.addAttribute("page", page);
		return "modules/partyassessment/partyAssessmentList";
	}

	@RequiresPermissions("partyassessment:partyAssessment:view")
	@RequestMapping(value = "form")
	public String form(PartyAssessment partyAssessment, Model model) {
		AssessmentTime at=new AssessmentTime();
		at.setState("1");
		at.setDelFlag("0");
		List<AssessmentTime> assessmentlist=assessmentTimeService.findList(at);
		PartyType pt=new PartyType();
		pt.setState("1");
		pt.setDelFlag("0");
		List<PartyType> partyTypelist = partyTypeService.findList(pt);
		model.addAttribute("assessmentlist", assessmentlist);
		model.addAttribute("partyTypelist", partyTypelist);
		model.addAttribute("partyAssessment", partyAssessment);
		return "modules/partyassessment/partyAssessmentForm";
	}

	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "save")
	public String save(PartyAssessment partyAssessment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partyAssessment)){
			return form(partyAssessment, model);
		}
		partyAssessmentService.save(partyAssessment);
		addMessage(redirectAttributes, "保存党组织考核成功");
		return "redirect:"+Global.getAdminPath()+"/partyassessment/partyAssessment/?repage";
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "delete")
	public String delete(PartyAssessment partyAssessment, RedirectAttributes redirectAttributes) {
		partyAssessmentService.delete(partyAssessment);
		addMessage(redirectAttributes, "删除党组织考核成功");
		return "redirect:"+Global.getAdminPath()+"/partyassessment/partyAssessment/?repage";
	}

	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "updateStart")
	public String updateStart(PartyAssessment partyAssessment, RedirectAttributes redirectAttributes) {
		partyAssessmentService.updateStart(partyAssessment);
		addMessage(redirectAttributes, "启动党组织考核成功");
		return "redirect:"+Global.getAdminPath()+"/partyassessment/partyAssessment/?repage";
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "updateStop")
	public String updateStop(PartyAssessment partyAssessment, RedirectAttributes redirectAttributes) {
		partyAssessmentService.updateStop(partyAssessment);
		addMessage(redirectAttributes, "关闭党组织考核成功");
		return "redirect:"+Global.getAdminPath()+"/partyassessment/partyAssessment/?repage";
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:view")
	@RequestMapping(value = {"listPartyAssessmentInfo"})
	public String listPartyAssessmentInfo(PartyAssessmentInfo partyAssessmentInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		PartyAssessment partyAssessment=partyAssessmentService.get(partyAssessmentInfo.getPartyAssessmentId());
		model.addAttribute("partyAssessment", partyAssessment);
		Page<PartyAssessmentInfo> page = partyAssessmentInfoService.findPage(new Page<PartyAssessmentInfo>(request, response), partyAssessmentInfo); 
		model.addAttribute("page", page);
		return "modules/partyassessmentinfo/partyAssessmentInfoList";
	}

	@RequiresPermissions("partyassessment:partyAssessment:view")
	@RequestMapping(value = "formPartyAssessmentInfo")
	public String formPartyAssessmentInfo(PartyAssessmentInfo partyAssessmentInfo, Model model) {
		if(partyAssessmentInfo.getId()!=null&&!"".equals(partyAssessmentInfo.getId())){
			partyAssessmentInfo= partyAssessmentInfoService.get(partyAssessmentInfo.getId());	
		}
		model.addAttribute("partyAssessmentInfo", partyAssessmentInfo);
		return "modules/partyassessmentinfo/partyAssessmentInfoForm";
	}

	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "savePartyAssessmentInfo")
	public String savePartyAssessmentInfo(PartyAssessmentInfo partyAssessmentInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, partyAssessmentInfo)){
			return formPartyAssessmentInfo(partyAssessmentInfo, model);
		}
		partyAssessmentInfoService.save(partyAssessmentInfo);
		addMessage(redirectAttributes, "保存党组织考核信息成功");
		return "redirect:"+Global.getAdminPath()+"/partyassessment/partyAssessment/listPartyAssessmentInfo/?partyAssessmentId="+
		partyAssessmentInfo.getPartyAssessmentId();
	}
	
	@RequiresPermissions("partyassessment:partyAssessment:edit")
	@RequestMapping(value = "deletePartyAssessmentInfo")
	public String deletePartyAssessmentInfo(PartyAssessmentInfo partyAssessmentInfo, RedirectAttributes redirectAttributes) {
		partyAssessmentInfo=partyAssessmentInfoService.get(partyAssessmentInfo);
		String partyAssessmentId=partyAssessmentInfo.getPartyAssessmentId();
		partyAssessmentInfoService.delete(partyAssessmentInfo);
		addMessage(redirectAttributes, "删除党组织考核信息成功");
		return "redirect:"+Global.getAdminPath()+"/partyassessment/partyAssessment/listPartyAssessmentInfo/?partyAssessmentId="+partyAssessmentId;
	}
}